﻿var movielist = [];
function addInitialEvents() {
    addEventToCategoryListItems();
    addEventsToPeliContainer();
    addMovieRequestEvent();
    addMovieCancelRequest();
}

function addEventToCategoryListItems() {

    $(".peliculas_categorialistitem").click(function (event) {
        for (i = 0; i < $(this).children().size() ; i++) {
            if ($(this).children()[i].tagName == "INPUT") {
                //solo debera haber un input
                $var_id_data_search_peli_category_onethat010011 = $(this).children()[i].value;
                switch ($var_id_data_search_peli_category_onethat010011) {
                    case 'belic': $var_id_data_search_peli_category_onethat010011 = 6; break;
                    case 'anim': $var_id_data_search_peli_category_onethat010011 = 4; break;
                    case 'acci': $var_id_data_search_peli_category_onethat010011 = 3; break;
                    case 'romc': $var_id_data_search_peli_category_onethat010011 = 12; break;
                    case 'infam': $var_id_data_search_peli_category_onethat010011 = 10; break;
                    case 'suspn': $var_id_data_search_peli_category_onethat010011 = 13; break;
                    case 'avent': $var_id_data_search_peli_category_onethat010011 = 5; break;
                    case 'cific': $var_id_data_search_peli_category_onethat010011 = 7; break;
                    case 'fanta': $var_id_data_search_peli_category_onethat010011 = 11; break;
                    case 'terro': $var_id_data_search_peli_category_onethat010011 = 14; break;
                    case 'drama': $var_id_data_search_peli_category_onethat010011 = 9; break;
                    case 'comedia': $var_id_data_search_peli_category_onethat010011 = 8; break;
                    default: break;
                }

                //elemento que se deberabuscar

                getMovies($var_id_data_search_peli_category_onethat010011);

            }

        }
    });
}

function addEventsToPeliContainer() {

    $(".peliculas_pelicontainer").click(function (event) {
        $("#pelis_pelisinfo").height($(document).height());
        $("#pelis_pelisinfo").fadeIn();
        for (i = 0; i < $(this).children().size() ; i++) {
            if ($(this).children()[i].tagName == "INPUT") {
                //solo debera haber un input
                $var_id_data_search_peli_onethat010011 = $(this).children()[i].value;
                //elemento que se deberabuscar
                getMovie($var_id_data_search_peli_onethat010011);


            }

        }
    });
}

function addFinalRequestCommit() {
    $("#btncommitrequest").click(function () {
        if (movielist.length > 0) {//redirigir
            if (confirm("Confirmar Solicitud")) {
                location.href = "../Default.aspx";
            }

        } else {
            alert("Debes elegir una pelicula para alquilar");
        }
    });


}


function showdetails(a) {
    //aqui se hara la peticion de ajax con respecto al value hidden que tendra el 

}

function getMovies(dmovie) {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var url = "peliculas.aspx?m=getmovies&d=" + dmovie;
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

            $("#peliculas_centro").html(xmlhttp.responseText);
            addEventsToPeliContainer();

        }
    }

}

function getMovie(dmovie) {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var url = "peliculas.aspx?m=getmovie_d&d=" + dmovie;
    xmlhttp.open("GET", url, true);
    xmlhttp.send(null);
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            $("#pelis_pelisinfo").html(xmlhttp.responseText);
            addMovieRequestEvent();
            addMovieCancelRequest();
            addXCancelRequest();
        }
    }
}


function addMovieRequestEvent() {

    $("#sendreq").click(function () {
        for (i = 0; i < $("#pelis_pelisinfo").children().size() ; i++) {
            if ($("#pelis_pelisinfo").children()[i].tagName == "INPUT") {
                //solo debera haber un input
                $var_id_data_search_peli_onethat010011 = $("#pelis_pelisinfo").children()[i].value;
                //aqui es donde se agrega la solicitud de pelicula
                // alert($var_id_data_search_peli_onethat010011);AGREGAR UNA PELICULA A LA LISTA
                addMovieToList($var_id_data_search_peli_onethat010011);
            }

        }
    });

}

function addMovieCancelRequest() {
    $("#btncancel").click(function () {
        $("#pelis_pelisinfo").fadeOut();
        $("#pelis_pelisinfo").html("");
        checkIfAnyMovie();
    });

}

function addXCancelRequest() {
    $("#close").click(function () {
        $("#pelis_pelisinfo").fadeOut();
        $("#pelis_pelisinfo").html("");
        checkIfAnyMovie();
    });
}

function addMovieToList(mov) {
    for (ii = 0; ii < movielist.length; ii++) {
        if (movielist[ii] == mov) {
            alert("Ya habeis seleccionado esta");
            return false;
        }

    }
    if (confirm("Desea solicitar esta pelicula?")) {


        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        var url = "peliculas.aspx?m=add_to_list&d=" + mov;
        xmlhttp.open("GET", url, true);
        xmlhttp.send(null);
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {



                alert("Agregado correctamente");
                movielist.push(mov);

                checkIfAnyMovie();
            }
        }
    }
}

function checkIfAnyMovie() {
    if (movielist.length > 0) {
        $("#btncommitrequest").fadeIn('slow');
        addFinalRequestCommit();
    }
    else {
        $("#btncommitrequest").hide();
    }
}